package com.itheima.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

// 导入对应SMS模块的client
import com.tencentcloudapi.sms.v20210111.SmsClient;

// 导入要请求接口对应的request response类
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

/**
 * @ClassName: SMSUtils
 * @Description: 腾讯云发送短信
 * @Author: 热爱生活の李
 * @Date: 2022/2/27 16:41
 */
public class SMSUtils {
    /*
    * @MethodName: sendShortMessage
    * @Description: 发送短信工具
    * @Params: secretId secretKey 腾讯云密钥
    * @Params: sdkAppId signName 短信应用Id,签名内容
    * @Params: templateId phoneNumberSet templateParamSet 模板Id,下发手机号,模板参数
    * @Author: 热爱生活の李
    */
    public static void sendShortMessage(String secretId,
                                        String secretKey,
                                        String sdkAppId,
                                        String signName,
                                        String templateId,
                                        String phoneNumberSet,
                                        String templateParamSet){
        Credential cred = new Credential(secretId,secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(sdkAppId);
        req.setSignName(signName);
        String sessionContext = "xxx";
        req.setSessionContext(sessionContext);
        req.setTemplateId(templateId);
        req.setPhoneNumberSet(new String[]{phoneNumberSet});
        req.setTemplateParamSet(new String[]{templateParamSet});
        try {
            SendSmsResponse res = client.SendSms(req);
        } catch (TencentCloudSDKException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendShortMessage("AKIDpUt1sTnqbw1pMcbJkunPkob6KGwMVMxH", "3pKYVBZhY7PQBgurYy70cx1nJZXEeV6P",
                        "1400641295","似梦的苏烟个人网",
                       "1321953","15883503354",
                                 "9658");
    }
}

